package com.lawyers.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.lawyers.common.UserVO;
import com.lawyers.database.DBLogin;
import com.lawyers.form.LoginForm;

public class LoginAction extends Action{
	
	private static Logger logger = Logger.getLogger(LoginAction.class);
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception
	{
		logger.info("Hi");
		String forward = "display";
		LoginForm loginForm = (LoginForm) form;
		String accion = loginForm.getAccion();
		
		if("aceptar".equalsIgnoreCase(accion)){
			UserVO user = DBLogin.loguear(loginForm.getUsername(), loginForm.getPassword());
			if(user != null){
				request.getSession().setAttribute("USUARIO", user);
				return mapping.findForward("aceptar");
			}
			else{
				loginForm.setExitoso("false");
				loginForm.setPassword("");
				loginForm.setUsername("");
			}
		}else{
			loginForm.setExitoso("");
			loginForm.setUsername("");
			loginForm.setPassword("");
		}
		return mapping.findForward(forward);
	}

}
